{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(path):\n",
    "    '''\n",
    "    加载数据，返回json数组.\n",
    "    '''\n",
    "    data = []\n",
    "    data_lines = open(path, encoding='utf-8').readlines()\n",
    "    for line in data_lines:\n",
    "        line_json = json.loads(line)\n",
    "        if len(line_json['postag']) == 0:\n",
    "            continue\n",
    "        if 'spo_list' in line_json.keys() and len(line_json['spo_list']) == 0:\n",
    "            continue\n",
    "        data.append(line_json)\n",
    "    return data\n",
    "train_data = load_data('./train_data.json')\n",
    "dev_data = load_data('./dev_data.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 统计类别数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_type2num(datas):\n",
    "    type2num = {}\n",
    "    for data in datas:\n",
    "        spo_list = data['spo_list']\n",
    "        for spo in spo_list:\n",
    "            obj_type = spo['object_type']\n",
    "            sbj_type = spo['subject_type']\n",
    "#             if obj_type == \"机构\" or sbj_type == '机构':\n",
    "#                 print(data['text'])\n",
    "#                 print(data['spo_list'])\n",
    "            if obj_type not in type2num.keys():\n",
    "                type2num[obj_type] = 1\n",
    "            else:\n",
    "                type2num[obj_type] += 1\n",
    "            if sbj_type not in type2num.keys():\n",
    "                type2num[sbj_type] = 1\n",
    "            else:\n",
    "                type2num[sbj_type] += 1\n",
    "    return type2num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "type_num_train = get_type2num(train_data)\n",
    "type_num_dev = get_type2num(dev_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'人物': 292928, '影视作品': 100202, '目': 10327, '生物': 10327, 'Number': 5276, 'Date': 39704, '国家': 12800, '网站': 12876, '网络小说': 13126, '图书作品': 33848, '歌曲': 54654, '地点': 24147, '气候': 920, '行政区': 2014, 'Text': 20193, '历史人物': 7682, '学校': 11514, '企业': 19388, '出版社': 17706, '书籍': 17706, '音乐专辑': 10399, '城市': 972, '景点': 575, '电视综艺': 2879, '机构': 5309, '作品': 553, '语言': 145, '学科专业': 46}\n"
     ]
    }
   ],
   "source": [
    "print(type_num_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'地点': 2904, '人物': 36535, 'Date': 4961, '歌曲': 6614, '企业': 2509, '影视作品': 12505, '图书作品': 4393, '出版社': 2272, '书籍': 2272, '国家': 1717, 'Text': 2532, '目': 1268, '生物': 1268, '机构': 653, '网站': 1657, '网络小说': 1696, '音乐专辑': 1287, 'Number': 686, '学校': 1433, '历史人物': 951, '电视综艺': 379, '城市': 124, '景点': 77, '作品': 71, '行政区': 231, '气候': 103, '学科专业': 5, '语言': 15}\n"
     ]
    }
   ],
   "source": [
    "print(type_num_dev)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
